package tests;

import org.junit.Assert;
import org.junit.Before;
import org.junit.After;
import org.junit.Test;

import video.PlayList;

import java.io.File;
import java.util.*;


/**
 * @author Heitor Meira de Melo.
 */
public class PlayListTest {
	PlayList playlist;
	List<String> files;
	
	@Before
	public void setUp() throws Exception {
		playlist = new PlayList();
		files = new ArrayList<String>();
		files.add("Videos/Samples/bailey.flv");
		files.add("Videos/Samples/stinsonAwesome.flv");
		
	}
	
	@After
	public void clearUp(){
		playlist = null;
		files = null;
	}
	
	@Test(expected=Exception.class)
	public void testAddNullListFiles() throws Exception {
		playlist.addPlayList(null);
	}
	
	@Test
	public void testAddOneinvalidFile() throws Exception {
		List<String> l = new ArrayList<String>();
		l.add("Ivalid File");
		playlist.addPlayList(l);
		Assert.assertEquals("bailey.flv",playlist.getPlayList().get(0).getName());
	}
	
	@Test
	public void testAddListFiles() throws Exception {
		playlist.addPlayList(files);
		Assert.assertEquals(2, playlist.getPlayList().size());
	}
	
	@Test
	public void testGetPlayList() throws Exception {
		playlist.addPlayList(files);
		Assert.assertEquals(2, playlist.getPlayList().size());
		playlist.addPlayList(files);
		Assert.assertEquals(4, playlist.getPlayList().size());
	}
	
	@Test(expected=Exception.class)
	public void testSetNullListFiles() throws Exception {
		playlist.setPlayList(null);
	}
	
	@Test
	public void testSetListFiles() throws Exception {
		playlist.addPlayList(files);
		Assert.assertEquals(2, playlist.getPlayList().size());
		
		List<String> l = new ArrayList<String>();
		l.add("Videos/Samples/bailey.flv");
		l.add("Videos/Samples/stinsonAwesome.flv");
		l.add("Videos/Samples/bailey.flv");
		l.add("Videos/Samples/PagodeiroMacho.flv");

		playlist.setPlayList(l);
		
		Assert.assertEquals(3, playlist.getPlayList().size());
	}
	
	@Test(expected=Exception.class)
	public void testAddNullVideo() throws Exception {
		playlist.addVideo(null);
	}
	
	@Test(expected=Exception.class)
	public void testAddNonexistentVideo() throws Exception {
		playlist.addVideo(new File("Videos/Samples/PagodeiroMacho.flv"));
	}
	
	@Test
	public void testAddVideo() throws Exception {
		playlist.addPlayList(files);
		Assert.assertEquals(2, playlist.getPlayList().size());
		playlist.addVideo(new File("Videos/Samples/bailey.flv"));
		Assert.assertEquals(3, playlist.getPlayList().size());
	}
	
	@Test
	public void testRemoveNull() throws Exception {
		playlist.addPlayList(files);
		playlist.remove(null);
	}
	
	@Test
	public void testRemove() throws Exception {
		playlist.addPlayList(files);
		Assert.assertEquals(2, playlist.getPlayList().size());
		
		playlist.remove(new File("Videos/Samples/stinsonAwesome.flv"));
		Assert.assertEquals(1, playlist.getPlayList().size());
		
		playlist.remove(new File("Videos/Samples/PagodeiroMacho.flv"));
		
		Assert.assertEquals(1, playlist.getPlayList().size());
		
		
	}
	
	@Test
	public void testRemoveAll() throws Exception {
		playlist.addPlayList(files);
		playlist.removeAll();
		
		Assert.assertEquals(0, playlist.getPlayList().size());
		
		playlist.removeAll();
	}
	
	@Test
	public void testNext() throws Exception{
		playlist.addPlayList(files);
		Assert.assertEquals(0, playlist.curretPosition());
		playlist.next();
		Assert.assertEquals(1, playlist.curretPosition());
		playlist.next();
		Assert.assertEquals(0, playlist.curretPosition());
		playlist.next();
		Assert.assertEquals(1, playlist.curretPosition());
	}
	
	@Test
	public void testPreview() throws Exception{
		playlist.addPlayList(files);
		playlist.addPlayList(files);
		Assert.assertEquals(0, playlist.curretPosition());
		playlist.next();
		playlist.next();
		Assert.assertEquals(2, playlist.curretPosition());
		playlist.preview();
		Assert.assertEquals(1, playlist.curretPosition());
		playlist.preview();
		Assert.assertEquals(0, playlist.curretPosition());
		playlist.preview();
		Assert.assertEquals(3, playlist.curretPosition());
	}
	
	@Test
	public void testPositionToZero() throws Exception {
		playlist.addPlayList(files);
		playlist.addPlayList(files);
		Assert.assertEquals(0, playlist.curretPosition());
		playlist.next();
		playlist.next();
		Assert.assertEquals(2, playlist.curretPosition());
		playlist.positionToZero();
		Assert.assertEquals(0, playlist.curretPosition());
	}
	
	@Test
	public void testPosition() throws Exception {
		playlist.addPlayList(files);
		playlist.addPlayList(files);
		Assert.assertEquals(0, playlist.curretPosition());
		playlist.next();
		Assert.assertEquals(1, playlist.curretPosition());
		playlist.positionToZero();
		Assert.assertEquals(0, playlist.curretPosition());
		playlist.toPosition(4);
		Assert.assertEquals(4, playlist.curretPosition());
	}
	
	@Test(expected=Exception.class)
	public void testInvalidToPositionPositive() throws Exception {
		playlist.addPlayList(files);
		playlist.addPlayList(files);
		playlist.toPosition(5);
	}
	
	@Test(expected=Exception.class)
	public void testInvalidToPositionNegative() throws Exception {
		playlist.addPlayList(files);
		playlist.addPlayList(files);
		playlist.toPosition(-1);
	}
	
	@Test
	public void testCurrentVideo() throws Exception {
		playlist.addPlayList(files);
		playlist.addPlayList(files);
		Assert.assertEquals(new File("Videos/Samples/bailey.flv"), playlist.currentVideo());
		Assert.assertEquals(0, playlist.curretPosition());
		playlist.next();
		Assert.assertEquals(new File("Videos/Samples/stinsonAwesome.flv"), playlist.currentVideo());
		Assert.assertEquals(1, playlist.curretPosition());
	}
	
	@Test
	public void testClearPlayList() throws Exception {
		playlist.addPlayList(files);
		Assert.assertEquals(2, playlist.getPlayList().size());
		playlist.clearPlayList();
		Assert.assertEquals(0, playlist.getPlayList().size());
	}
	
	@Test
	public void testSetListFilesPosition() throws Exception {
		playlist.addPlayList(files);
		Assert.assertEquals(2, playlist.getPlayList().size());
		Assert.assertEquals(0, playlist.curretPosition());
		playlist.next();
		Assert.assertEquals(1, playlist.curretPosition());
		List<String> l = new ArrayList<String>();
		l.add("Videos/Samples/stinsonAwesome.flv");
		l.add("Videos/Samples/bailey.flv");
		l.add("Videos/Samples/stinsonAwesome.flv");
		l.add("Videos/Samples/PagodeiroMacho.flv");

		playlist.setPlayList(l);
		Assert.assertEquals(0, playlist.curretPosition());
		Assert.assertEquals(3, playlist.getPlayList().size());
	}
}
